//
//  buttonView.m
//  MiBaoTu
//
//  Created by 彭鹏 on 16/9/28.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBButtonView.h"

@implementation MBButtonView
{
    UIButton *_btn1;
    UIButton *_btn2;
    UIButton *_btn3;
    UIButton *_btn4;
    UIButton *_btn5;
    UISlider *_sliderBtn;
    CLShareManager *shareView;
}
- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setupUI];
}

-(instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    _btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self addSubview:_btn1];
    
    _btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self addSubview:_btn2];
    
    _btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self addSubview:_btn3];
    
    _btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self addSubview:_btn4];
    
        }
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    float itemWidth = 60*PERCEN;
    float itemHeight = itemWidth;
    float itemtop = 20*PERCEN;
    float itemJU = (self.width-itemWidth*4)/5.;
    UIImage *img1 = [UIImage imageNamed:@"shoucang_bf"];
     UIImage *img11 = [UIImage imageNamed:@"shoucang_3.0"];
    if (ISIPAD) {
         [_btn1 setImage:img11 forState:UIControlStateNormal];
    }else{
    [_btn1 setImage:img1 forState:UIControlStateNormal];
    }
    [_btn1 setImage:[UIImage imageNamed:@"quxiaoshoucang_bf"] forState:UIControlStateSelected];
    if (ISIPAD) {
            [_btn1 setImage:[UIImage imageNamed:@"quxiaoshoucang_3.0"] forState:UIControlStateSelected];
    }
    _btn1.frame = CGRectMake(itemJU, itemtop,itemWidth,itemHeight);
    if (ISIPAD) {
         _btn1.frame = CGRectMake(40*PERCEN, itemtop,itemWidth,itemHeight);
    }
    [_btn1 addTarget:self action:@selector(shoucangBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    NSString *modelType = [[NSUserDefaults standardUserDefaults]objectForKey:@"player_model_type"];
    if (modelType.length == 0) {
        _playType = PLayerModelType_Shunxu;
         [_btn2 setImage:[UIImage imageNamed:@"shunxu_dt"] forState:UIControlStateNormal];
        if (ISIPAD) {
            [_btn2 setImage:[UIImage imageNamed:@"shunxu"] forState:UIControlStateNormal];
        }
    } else {
        switch ([modelType intValue]) {
            case 1:
            {
                _playType = PLayerModelType_Shunxu;
                [_btn2 setImage:[UIImage imageNamed:@"shunxu_dt"] forState:UIControlStateNormal];
                if (ISIPAD) {
                [_btn2 setImage:[UIImage imageNamed:@"shunxu"] forState:UIControlStateNormal];
                }
       
            }
                break;
            case 2:
            {
                _playType = PLayerModelType_SuiJi;
                [_btn2 setImage:[UIImage imageNamed:@"suiji_dt"] forState:UIControlStateNormal];
                if (ISIPAD) {
                     [_btn2 setImage:[UIImage imageNamed:@"suiji"] forState:UIControlStateNormal];
                }
            }
                break;
            case 3:
            {
                _playType = PLayerModelType_DanQu;
                [_btn2 setImage:[UIImage imageNamed:@"danqu_dt"] forState:UIControlStateNormal];
                if (ISIPAD) {
                      [_btn2 setImage:[UIImage imageNamed:@"danqu"] forState:UIControlStateNormal];
                }
            }
                break;
                
            default:
                break;
        }
    }
    _btn2.frame = CGRectMake(_btn1.right+itemJU, itemtop,itemWidth, itemHeight);
     [_btn2 addTarget:self action:@selector(danquBtnClick) forControlEvents:UIControlEventTouchUpInside];
    UIImage *img3 = [UIImage imageNamed:@"weixiazai_dt"];
    UIImage *img33 = [UIImage imageNamed:@"weixiazai"];
    if (ISIPAD) {
        [_btn3 setImage:img33 forState:UIControlStateNormal];
        [_btn3 setImage:[UIImage imageNamed:@"yixiazai"] forState:UIControlStateSelected];
        [_btn3 setImage:[UIImage imageNamed:@"xiazaizhong"] forState:UIControlStateDisabled];
    } else {
        [_btn3 setImage:img3 forState:UIControlStateNormal];
        [_btn3 setImage:[UIImage imageNamed:@"yixiazai_bf"] forState:UIControlStateSelected];
        [_btn3 setImage:[UIImage imageNamed:@"xiazaizhong_dt"] forState:UIControlStateDisabled];
    }
    _btn3.frame = CGRectMake(_btn2.right+itemJU,itemtop,itemWidth, itemHeight);
     [_btn3 addTarget:self action:@selector(xiazaiBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIImage *img4 = [UIImage imageNamed:@"fenxiang_dt"];
     UIImage *img44 = [UIImage imageNamed:@"fenxiang_3.0"];
    [_btn4 setImage:img4 forState:UIControlStateNormal];
    if (ISIPAD) {
         [_btn4 setImage:img44 forState:UIControlStateNormal];
    }
    _btn4.frame = CGRectMake(_btn3.right+itemJU, itemtop, itemWidth, itemHeight);
     [_btn4 addTarget:self action:@selector(fenxiangBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
}

- (void)onUpdateCollection:(BOOL)isCollection
{
    _btn1.selected = isCollection;
    if (isCollection==YES) {
        [_btn1 setImage:[UIImage imageNamed:@"quxiaoshoucang_bf"] forState:0];
        if (ISIPAD) {
            [_btn1 setImage:[UIImage imageNamed:@"quxiaoshoucang_3.0"] forState:0];
        }
    }else{
        [_btn1 setImage:[UIImage imageNamed:@"shoucang_bf"] forState:0];
        if (ISIPAD) {
            [_btn1 setImage:[UIImage imageNamed:@"shoucang_3.0"] forState:0];

        }
        
    }
    
}

- (void)onUpdateDown:(BOOL)isDown withModel:(Music *)currentMusic
{
    _isDownLoad = isDown;
    if (_isDownLoad == YES) {
        _btn3.selected = YES;
    } else {
        //未已下载好 判断是否正在下载
        NSMutableArray *mMusicArr = [[NSMutableArray alloc]init];
        [mMusicArr addObjectsFromArray:[UserManager standerDefult].downingMusicCountArr];
        [mMusicArr addObjectsFromArray:[UserManager standerDefult].waitDownMusicCountArr];
        if (mMusicArr.count > 0) {
            for (DownMusicModel *model in mMusicArr) {
                if ([model.musicID intValue] == [currentMusic.ID intValue] && [model.musicType intValue] == [currentMusic.musicType intValue]) {
                    _btn3.selected = YES;
                    _btn3.enabled = NO;
                } else {
                    _btn3.selected = NO;
                    _btn3.enabled = YES;
                }
            }
        } else {
            _btn3.selected = NO;
            _btn3.enabled = YES;
        }
    }
}

- (void)onEnabledUpdateDown
{
    _btn3.selected = YES;
    _btn3.enabled = NO;
}

- (void)onUpdatePlayMusicType:(PLayerModelType)type
{
    
    if (_playType == PLayerModelType_Shunxu) {
        
        [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"player_model_type"];
        [_btn2 setImage:[UIImage imageNamed:@"shunxu_dt"] forState:UIControlStateNormal];
        if (ISIPAD) {
              [_btn2 setImage:[UIImage imageNamed:@"shunxu"] forState:UIControlStateNormal];
        }
    }
    else if (_playType == PLayerModelType_SuiJi) {
        
        [[NSUserDefaults standardUserDefaults]setObject:@"2" forKey:@"player_model_type"];
        [_btn2 setImage:[UIImage imageNamed:@"suiji_dt"] forState:UIControlStateNormal];
        if (ISIPAD) {
              [_btn2 setImage:[UIImage imageNamed:@"suiji"] forState:UIControlStateNormal];
        }
    }
    else if (_playType == PLayerModelType_DanQu) {
        
        [[NSUserDefaults standardUserDefaults]setObject:@"3" forKey:@"player_model_type"];
        [_btn2 setImage:[UIImage imageNamed:@"danqu_dt"] forState:UIControlStateNormal];
        if (ISIPAD) {
                [_btn2 setImage:[UIImage imageNamed:@"danqu"] forState:UIControlStateNormal];
        }
    }
    
    [[NSUserDefaults standardUserDefaults]synchronize];
}

-(void)shoucangBtnClick {
    NSLog(@"收藏");
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectCollectionMusicWithValue:)]) {
        [_delegate onSelectCollectionMusicWithValue:@""];
    }
}

- (void)danquBtnClick {
    if (_playType == PLayerModelType_Shunxu) {
        _playType = PLayerModelType_SuiJi;
    }
    else if (_playType == PLayerModelType_SuiJi) {
        _playType = PLayerModelType_DanQu;
    }
    else if (_playType == PLayerModelType_DanQu) {
        _playType = PLayerModelType_Shunxu;
    }
    
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectPLayMusicTypeWithValue:)]) {
        [_delegate onSelectPLayMusicTypeWithValue:_playType];
    }
}

- (void)xiazaiBtnClick {
    NSLog(@"下载");
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectDownMusicWithValue:)]) {
        [_delegate onSelectDownMusicWithValue:[NSNumber numberWithBool:_btn3.selected]];
    }
}
- (void)fenxiangBtnClick {
    NSLog(@"分享");
    
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectShareMusicWithValue:)]) {
        [_delegate onSelectShareMusicWithValue:@""];
    }
}

@end
